//
//  DEWPhoneAddFriendController.m
//  OtherDemo
//
//  Created by lianjie on 2018/7/25.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWPhoneAddFriendController.h"
#import "DEWNewFriendCell.h"
#import <AddressBook/AddressBook.h>
#import <AddressBook/ABRecord.h>
#import <Contacts/Contacts.h>
@interface DEWPhoneAddFriendController ()<UITableViewDelegate,UITableViewDataSource,DEWNewFriendDelegate>

@property (nonatomic ,strong) UITableView *table;

///手机联系人
@property (nonatomic ,strong) NSMutableArray *phoneFriendList;
///手机号对应系统数据
@property (nonatomic ,strong) NSMutableArray *dataList;
@end

@implementation DEWPhoneAddFriendController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"查看手机联系人";
    [self loadData];
    [self createView];
}
#pragma mark ----requestData
-(void)loadData
{
    [self getUserPhoneNumberList];
    [self getmyAddressbook];
    
}

#pragma mark ---获取手机号
- (void) getUserPhoneNumberList
{
    if ([[UIDevice currentDevice].systemVersion floatValue]>=9.0){
        
        CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
        if (authorizationStatus == CNAuthorizationStatusNotDetermined) {
            CNContactStore *contactStore = [[CNContactStore alloc] init];
            [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
                if (granted) {
                    
                } else {
                    NSLog(@"授权失败, error=%@", error);
                }
            }];
        }
        
    }else
    {
        [self showHint:@"请升级系统"];
    }
}


- (void)getmyAddressbook {
    CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (authorizationStatus == CNAuthorizationStatusAuthorized) {
        NSLog(@"没有授权...");
    }
    
    NSMutableDictionary *_myDict = [[NSMutableDictionary alloc]init];
    
    // 获取指定的字段,并不是要获取所有字段，需要指定具体的字段
    NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
    CNContactStore *contactStore = [[CNContactStore alloc] init];
    
    [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
        NSLog(@"-------------------------------------------------------");
        
        NSString *givenName = contact.givenName;
        NSString *familyName = contact.familyName;
        NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
        
        NSString *nameStr = [NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName];
        
        NSArray *phoneNumbers = contact.phoneNumbers;
        
        for (CNLabeledValue *labelValue in phoneNumbers) {
            NSString *label = labelValue.label;
            phoneNumbers = labelValue.value;
            
//            NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);
            
            
        }
        
//        [_myDict setObject:phoneNumber.stringValue forKey:nameStr];
        
        
        //    *stop = YES; // 停止循环，相当于break；
        
    }];
    
    
    NSLog(@"mydict is ==== %@",_myDict);
    
}

#pragma mark ---initView
-(void)createView
{
    [self.view addSubview:self.table];
    
    
}

#pragma mark ---添加好友
-(void)DEWNewFriendCell:(DEWBaseTableViewCell *)cell didCellAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

#pragma mark ---tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if (section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = UIColorFromRGB(0x999999);
        cell.textLabel.text = @"搜索手机联系人";
        
        return cell;
        
    }else{
        DEWNewFriendCell *cell = [[DEWNewFriendCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"friendCell"];
        
        cell.indexPath = indexPath;
        cell.delegate = self;
        cell.friendInfo = nil;
        
        return cell;
        
    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75.0f;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.frame = CGRectMake(0, 0, DEWScreenWidth, 20.0f);
    view.backgroundColor = [UIColor clearColor];
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 0, 100, 20.0f)];
    lab.font = [UIFont systemFontOfSize:14.0f];
    
    lab.textColor = UIColorFromRGB(0x999999);
    
    lab.text = @"O";
    
    [view addSubview:lab];
    
    
    return view;
}

#pragma mark --lazy
-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEWScreenWidth, DEWScreenHeight-(NAVH)) style:UITableViewStyleGrouped];
        
        _table.delegate = self;
        
        _table.dataSource = self;
        
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _table;
    
}

-(NSMutableArray *)phoneFriendList
{
    if (!_phoneFriendList) {
        _phoneFriendList = [NSMutableArray array];
    }
    return _phoneFriendList;
}

-(NSMutableArray *)dataList
{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
